<!DOCTYPE html>
<html>
<head>
    <header th:replace="layout/admin/head :: head"></header>
    <!--日期选择器相关-->
    <script type="text/javascript" th:src="@{~/js/bootstrap-datetimepicker.js}"></script>
    <link th:href="@{~/css/bootstrap-datetimepicker.css}" rel="stylesheet"/>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-2"><h2>配置修改</h2></div>
    </div>
    <div class="row">
        <div class="col-md-2">
            <div id="toolbar" class="btn-group">
                <button id="btn_edit" type="button" class="btn btn-default">
                    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
                </button>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <table id="my_table"></table>
        </div>
    </div>
</div>


<div class="modal fade" id="newModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop='static'>
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true"></span></button>
                <h4 class="modal-title" id="myModalLabel">配置信息修改</h4>
            </div>
            <div class="modal-body">
                <form id="biz_form">
                    <div class="form-group">
                        <label for="id">id</label>
                        <input type="text" class="form-control" id="id" placeholder="id" name="id"
                               readonly="true">
                    </div>
                    <div class="form-group">
                        <label for="apiPath">api地址</label>
                        <input type="text" class="form-control" id="apiPath" placeholder="api地址"
                               name="apiPath" >
                    </div>
                    <div class="form-group">
                        <label for="author">作者</label>
                        <input type="text" class="form-control" id="author" placeholder="作者" name="author">
                    </div>
                    <div class="input-group">
                        <label for="cover">是否覆盖</label>
                        <input type="text" class="form-control" id="cover" placeholder="是否覆盖" name="cover">
                    </div>
                    <div class="form-group">
                        <label for="moduleName">项目名称</label>
                        <input type="text" class="form-control" id="moduleName" placeholder="联系人" name="moduleName">
                    </div>
                    <div class="form-group">
                        <label for="pack">包名称</label>
                        <input type="text" class="form-control" id="pack" placeholder="联系人" name="pack">
                    </div>
                    <div class="form-group">
                        <label for="path">路径</label>
                        <input type="text" class="form-control" id="path" placeholder="联系人" name="path">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button id="btn_save" type="button" class="btn btn-success" >确定</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">


    $(function () {
        //1.初始化Table
        var oTable = new TableInit();
        oTable.Init();
    });

    var TableInit = function () {
        var oTableInit = new Object();
        //初始化Table
        oTableInit.Init = function () {
            $('#my_table').bootstrapTable({
                url: '/api/genConfig',         //请求后台的URL（*）
                method: 'get',                      //请求方式（*）
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: false,                   //是否显示分页（*）
                // sortable: false,                     //是否启用排序
                // sortName: 'id',
                // sortOrder: "desc",                   //排序方式
                // queryParams: oTableInit.queryParams,//传递参数（*）
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                // pageNumber: 1,                       //初始化加载第一页，默认第一页
                // pageSize: 10,                       //每页的记录行数（*）
                // pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                strictSearch: true,
                showColumns: false,                  //是否显示所有的列
                showRefresh: false,                  //是否显示刷新按钮
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "id",                     //每一行的唯一标识，一般为主键列
                showToggle: false,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: false,                   //是否显示父子表
                singleSelect: true,
                // rowStyle: function (row, index) {       //收货信息不匹配的，改变背景色
                //     if (row.isMatch == 0) {
                //         return {classes: 'danger'}
                //     } else if (row.isMatch == 1) {
                //         return {classes: 'success'}
                //     } else {
                //         return {classes: 'info'}
                //     }
                // },
                // classes:'table-striped',
                responseHandler: function (res) {
                    var dataArray = new Array();
                    dataArray[0] = res.data;
                    return {

                        "total": 999,
                        "rows": dataArray
                    }
                },

                columns: [{
                    checkbox: true
                }, {
                    field: 'id',
                    title: 'id',
                    visible: false,
                }, {
                    field: 'pack',
                    title: '包路径'
                }, {
                    field: 'moduleName',
                    title: '模块名'
                }, {
                    field: 'path',
                    title: '前端文件路径'
                }, {
                    field: 'apiPath',
                    title: '前端文件路径'
                }, {
                    field: 'author',
                    title: '作者'
                }, {
                    field: 'cover',
                    title: '是否覆盖'
                }]
            });
        }


        return oTableInit;
    };

    $('#doSearch').on('click', function () {
        var opt = {
            silent: true
        };
        $("#my_table").bootstrapTable('refresh', opt);
    })

    var opt = {
        silent: true
    };


    //模态窗口（保存）
    $('#btn_save').on('click',function () {
        var tokenData= JSON.stringify($("#biz_form").serializeObject());
        $.ajax({
            url:"/api/genConfig",
            type:"PUT",
            data:tokenData,
            contentType:"application/json",
            success:function(data){
                alert("保存成功");
            },
            error:function (jqXHR, textStatus, errorThrown) {
                alert("保存失败"+jqXHR.responseJSON.message);
            }
        });
    })

    // 修改
    $('#btn_edit').on('click',function () {
        //先得选中行
        var row=$("#my_table").bootstrapTable('getSelections');
        if(row!=null && row.length>0)
        {
            $('#newModal').on('show.bs.modal',function () {
                $('#id').val(row[0].id);

                $('#apiPath').val(row[0].apiPath);
                $('#author').val(row[0].author);
                $('#cover').val(row[0].cover);
                $('#moduleName').val(row[0].moduleName);
                $('#pack').val(row[0].pack);
                $('#path').val(row[0].path);
            }).modal();
        }
        else{
            alert("请先选择要修改的数据")
        }
    })
    // 关闭模态窗口时进行刷新
    $('#newModal').on('hide.bs.modal', function () {
        $("#my_table").bootstrapTable('refresh', opt);
    })


    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }
</script>


<script type="text/javascript">
    $.fn.datetimepicker.dates['zh-CN'] = {
        days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
        daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六", "周日"],
        daysMin: ["日", "一", "二", "三", "四", "五", "六", "日"],
        months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthsShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        today: "今天",
        suffix: [],
        meridiem: ["上午", "下午"]
    };

    $('.form_datetime').datetimepicker({
        language: 'zh-CN',
        weekStart: 1,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1,
        format: 'yyyy-mm-dd hh:ii:ss'
    });

    $(function () {
        //给日期控件赋初始值
        var endDate = new Date();
        var beginDate = endDate.getDate() - 7;
        $("#begin_date").attr("value", beginDate);
        $("#end_date").attr("value", endDate);
    })
</script>

</body>
</html>